package com.ruoyi.web.controller.function;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.AddressUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.entity.GlPeople;
import com.ruoyi.system.entity.IsolationInfo;
import com.ruoyi.system.entity.Location;
import com.ruoyi.system.service.IsolationInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import org.apache.commons.lang.StringUtils;

import javax.servlet.http.HttpServletResponse;

/**
 * @author Y·C
 * @version 1.0.0
 * @ClassName IsolationInfoController.java
 * @Description TODO
 * @createTime 2023年11月18日 15:00:00
 */
@Api(tags = "隔离点管理接口")
@RestController
@RequestMapping("/isolationInfo")
public class IsolationInfoController extends BaseController {


    @Autowired
    private IsolationInfoService isolationInfoService;

    /**
     * 查询隔离点信息
     * */
    @ApiOperation(value = "查询隔离点信息")
    @GetMapping("/getIsolationInfoList")
    @Anonymous
    public AjaxResult getIsolationInfoList() {
        List<IsolationInfo> isolationInfos = isolationInfoService.getIsolationInfoList();
        return AjaxResult.success(isolationInfos);
    }

    /**
     * 根据id获取隔离点信息
     * */
    @ApiOperation(value = "根据id获取隔离点信息")
    @GetMapping("/getIsolationInfoById/{id}")
    @Anonymous
    public AjaxResult getIsolationInfoById(@PathVariable("id") Long id) {
       IsolationInfo isolationInfo = isolationInfoService.getIsolationInfoById(id);
        return AjaxResult.success(isolationInfo);
    }

    /**
     *查看全部隔离点信息
     * */
    @ApiOperation(value = "查看全部隔离点信息")
    @GetMapping("/getAllIsolationInfoList")
    @Anonymous
    public TableDataInfo getAllIsolationInfoList(IsolationInfo isolationInfo) {
        startPage();  // 此方法配合前端完成自动分页
        List<IsolationInfo> isolationInfos = isolationInfoService.getAllIsolationInfoList(isolationInfo);
        return getDataTable(isolationInfos);
    }

    /**
     * 删除多条隔离点信息
     * */
    @ApiOperation(value = "删除多条隔离点信息")
    @DeleteMapping("/deleteIsolationInfoByIds")
    @Anonymous
    public AjaxResult deleteIsolationInfoByIds(@RequestBody List<Long> ids) {
        return AjaxResult.success(isolationInfoService.removeByIds(ids));
    }

    /**
     * 新增隔离点信息
     * */
    @ApiOperation(value = "新增隔离点信息")
    @PostMapping("/addIsolationInfo")
    @Anonymous
    public AjaxResult addIsolationInfo(@RequestBody IsolationInfo isolationInfo) {
        LambdaQueryWrapper<IsolationInfo> isolationInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        isolationInfoLambdaQueryWrapper.eq(IsolationInfo::getIsolationName,isolationInfo.getIsolationName());
        IsolationInfo isolationInfo1 = isolationInfoService.getOne(isolationInfoLambdaQueryWrapper);
        if (isolationInfo1 != null) {
            return AjaxResult.error("该隔离点已存在");
        }
        return AjaxResult.success(isolationInfoService.addIsolationInfo(isolationInfo));
    }

    /**
     * 修改隔离点信息
     * */
    @ApiOperation(value = "修改隔离点信息")
    @PutMapping("/updateIsolationInfo")
    @Anonymous
    public AjaxResult updateIsolationInfo(@RequestBody IsolationInfo isolationInfo) {
        return AjaxResult.success(isolationInfoService.updateIsolationInfo(isolationInfo));
    }


    @ApiOperation("通过地点获取经纬度添加隔离地点")
    @Anonymous
    @GetMapping("/info/{formattedAddress}")
    public AjaxResult result( @PathVariable("formattedAddress") String formattedAddress){

        String result = AddressUtils.getGeographyInfoByAddress(formattedAddress);

        String s1 = StringUtils.substringAfter(result, "location\":\"");
        String s2 = StringUtils.substringBefore(s1, "\",\"le");
        //经度
        String s3 = StringUtils.substringBefore(s2,",");
        //纬度
        String s4 = StringUtils.substringAfter(s2,",");

        Location location = new Location();
        location.setIsolationLongitude(s3);
        location.setIsolatedLatitude(s4);
        System.out.println("经度："+s3+"纬度："+s4);
        return AjaxResult.success(location);
    }


    @Anonymous
    @ApiOperation(value = "导出隔离人员信息")
    @RequestMapping("/export")
    public void export(HttpServletResponse response, IsolationInfo isolationInfo) {
        List<IsolationInfo> allIsolationInfoList = isolationInfoService.getAllIsolationInfoList(isolationInfo);
        ExcelUtil<IsolationInfo> util = new ExcelUtil<IsolationInfo>(IsolationInfo.class);
        util.exportExcel(response,allIsolationInfoList, "隔离点数据");
    }
}
